﻿using StudentManagement.Extensions.Pagenation;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Models;
using System;
using System.Linq;
using System.Linq.Dynamic.Core;

namespace StudentManagement.Services
{
    public class StudentService : IStudentService
    {
        private readonly IRepository<Student> stuRepository;

        public StudentService(IRepository<Student> studentRepository)
        {
            this.stuRepository = studentRepository;
        }

        public PagedData<Student> GetPagedData(PageParameters pageParameters)
        {
            var data = stuRepository.GetAll();
            if (! String.IsNullOrEmpty(pageParameters.SortBy)) {
                data = data.OrderBy(pageParameters.SortBy);
            }
            if(! String.IsNullOrEmpty(pageParameters.SearchString)) {
                data = data.Where(s => s.Name.Contains(pageParameters.SearchString) || s.Email.Contains(pageParameters.SearchString));
            }

            return new PagedData<Student>(data, pageParameters.PageIndex, pageParameters.PageSize);
        }
    }
}
